import { createRouter, createWebHistory } from 'vue-router'

const router = createRouter({
//   history: createWebHistory(import.meta.env.BASE_URL),
     history: createWebHistory(process.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue')
    },
    {
      path: '/',
      name: 'Layout',
      component: () => import('../views/Layout.vue'),
      redirect: '/product',
      children: [
        {
          path: '/product',
          name: 'Product',
          component: () => import('../views/Product.vue')
        }
      ]
    }
  ]
})

// 路由守卫
router.beforeEach((to, from, next) => {
  const admin = localStorage.getItem('admin')
  if (to.path === '/login') {
    next()
  } else {
    admin ? next() : next('/login')
  }
})

export default router
